package com.ruoyi.oos.overrun.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.oos.overrun.service.IOverrunRecordCalSelfService;
import com.ruoyi.oos.self.domain.OverrunRecordSelf;
import com.ruoyi.oos.self.domain.TagConfigSelf;
import com.ruoyi.oos.self.mapper.OverrunRecordSelfMapper;
import com.ruoyi.oos.self.mapper.TagConfigSelfMapper;

import cn.hz.pi.api.PiApiUtil;
import cn.hz.pi.api.util.DateUtil;
import cn.hz.pi.api.util.TagValue;

@Service("overrunRecordCalSelf")
public class OverrunRecordCalServiceSelfImpl  implements IOverrunRecordCalSelfService{
	
	@Autowired
	TagConfigSelfMapper configMapper;
	
	@Autowired
	OverrunRecordSelfMapper recordMapper;
   
	
	/**
	 * 单限值与多限值
	 */
	public void calOverrunRecordSelf(String startTime,String endTime ,Long userid) {
		TagConfigSelf config = new TagConfigSelf();
			config.setCalctype("1");
			config.setUserid(userid);
			startTime =startTime +" 00:00:00";
			endTime = endTime +" 23:59:59"; 
	   //循环测点
	   List<TagConfigSelf> configList = configMapper.selectTagConfigSelfList(config);
	   for (TagConfigSelf tagConfig : configList) {
		   try {
			   this.saveOverReturnRecord(tagConfig,startTime,endTime);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error,测点计算失败-》"+tagConfig.getTagname());
		}
		   
	  }
	}
	
	
	/**
	 * 根据开始时间 和 结束时间计算 测点
	 * @param tagConfig
	 * @param startTime
	 * @param endTime
	 */
	public void saveOverReturnRecord(TagConfigSelf tagConfig,String startTime,String endTime) {
		String tagName = tagConfig.getTagname();//测点名称
		String tagPiName = tagConfig.getPitagname();//pi名称
		String limitType = tagConfig.getLimittype();//限值类型
		Long allTime = DateUtil.minBetween(startTime,endTime);//总时间
		List<TagValue> list  =PiApiUtil.getValueList(null,tagPiName,startTime,endTime, allTime.intValue());
		//查询该测点所有的值
		
		if(limitType.equals("2") ||limitType.equals("1") ) {
			List<Map<String,Object>> tagList = configMapper.selectTagConfigListDetail(tagConfig);//查询多限值的上下限
			this.saveRecord(tagName, tagList, list, limitType,null,startTime,endTime,tagConfig.getUserid());//保存多限值
		}
		else if(limitType.equals("3")) {
			List<Map<String,Object>> tagList2 = configMapper.selectTagConfigListDetail2(tagConfig);//查询动态限值的上下限
			this.saveDongtaiRecord(tagName, tagList2, list, allTime, limitType,null,startTime,endTime,tagConfig.getUserid());//保存动态限值
		}
	}
	/**
	 * 动态限值业务处理
	 * @param tagName
	 * @param tagList
	 * @param list
	 * @param allTime
	 * @param limitType
	 * @param dutyid
	 * @param startTime
	 * @param endTime
	 */
	public void saveDongtaiRecord(String tagName,List<Map<String,Object>> tagList,List<TagValue> list,Long allTime,String limitType,Long dutyid,String startTime,String endTime,Long userid) {
		this.deleteRecord(tagName, startTime, endTime,limitType,userid);//删除
		for (Map<String, Object> map : tagList) {
			String type = map.get("type").toString();
			Object obj = map.get("pitag");
			if(obj!=null) {
				String calcstr = obj.toString();
				List<OverrunRecordSelf> upList =new ArrayList<>();
				List<TagValue> dongtailist =PiApiUtil.getCalCulation(null, calcstr, startTime, endTime,  allTime.intValue());
				if(type.equals("1") ) {
					  upList =this.getDongtaiUpList(list,dongtailist);
				}
				
				if(type.equals("2") ){
					// upList =this.getDongtaiUpList(list,dongtailist);
				}
				//Long groupId =Long.parseLong(map.get("groupid").toString());
				Long tagId =Long.parseLong(map.get("id").toString());
				String calctype = map.get("calctype").toString();
				String limittype = map.get("limittype").toString();
				String level = StringUtils.convertStr(map.get("level"));
				//String groupname =map.get("groupname").toString();
				String pitagname = map.get("pitagname").toString();
					for (OverrunRecordSelf recordBean : upList) {
					//	recordBean.setGroupid(groupId);
						recordBean.setTagid(tagId);
					//	recordBean.setIdentify(teamId);
						recordBean.setTimes(DateUtil.minDateBetween(recordBean.getStarttime(),recordBean.getEndtime()));
						//recordBean.setLimitval(limitval);
						recordBean.setLevel(level);
						recordBean.setCalctype(calctype);
						recordBean.setLimittype(limittype);
						recordBean.setDatatype(type);
						recordBean.setTagname(tagName);
						recordBean.setPitagname(pitagname);
						//recordBean.setGroupname(groupname);
						//recordBean.setTeamname(teamname);
						recordBean.setDutyid(dutyid);
						recordBean.setUserid(userid);
						recordMapper.insertOverrunRecordSelf(recordBean);
					}
			}
			
			
		}
	}

	/**
	 * 多限值业务处理
	 * @param tagName 测点名称
	 * @param tagList 测点列表
	 * @param list
	 * @param limitType
	 * @param dutyid
	 * @param startTime
	 * @param endTime
	 */
	public void saveRecord(String tagName,List<Map<String,Object>> tagList,
			List<TagValue> list,String limitType,Long dutyid,String startTime,String endTime,Long userid) {
		this.deleteRecord(tagName, startTime, endTime,limitType,userid);//删除该时间段所有值
		for (Map<String, Object> map : tagList) {//循环
			String type = map.get("type").toString();//判断上下线
			List<OverrunRecordSelf> upList =new ArrayList<>();
			Float limitval =Float.parseFloat(map.get("limitval").toString());
			Float endLimitval =Float.parseFloat(map.get("endlimitval").toString());
			if(type.equals("1") ) {
			if	(limitType.equals("1") ||  limitType.equals("2")) {
				  upList =this.getUpList(list,limitval,endLimitval);
			}
			}
			
			if(type.equals("2") ){
				if(limitType.equals("1") ||   limitType.equals("2")) {
					 upList =this.getDownList(list,limitval,endLimitval);
			 }
			}
			//Long groupId =Long.parseLong(map.get("groupid").toString());
			Long tagId =Long.parseLong(map.get("id").toString());
			String calctype = map.get("calctype").toString();
			String limittype = map.get("limittype").toString();
			String pitagname = map.get("pitagname").toString();
			String level = StringUtils.convertStr(map.get("level"));
		//	String groupname =map.get("groupname").toString();
				for (OverrunRecordSelf recordBean : upList) {
					//recordBean.setGroupid(groupId);
					recordBean.setTagid(tagId);
					//recordBean.setIdentify(teamId);
					recordBean.setTimes(DateUtil.minDateBetween(recordBean.getStarttime(),recordBean.getEndtime()));
					recordBean.setLimitval(limitval);
					 recordBean.setLevel(level);
					recordBean.setCalctype(calctype);
					recordBean.setLimittype(limittype);
					recordBean.setDatatype(type);
					recordBean.setTagname(tagName);
					recordBean.setPitagname(pitagname);
					//recordBean.setGroupname(groupname);
					//recordBean.setTeamname(teamname);
					recordBean.setDutyid(dutyid);
					recordBean.setUserid(userid);
					recordMapper.insertOverrunRecordSelf(recordBean);
				}
		}
	}
	
	
	public void deleteRecord(String tagName,String startTime,String endTime,String limitType,Long userid) {
		OverrunRecordSelf record = new OverrunRecordSelf();
		record.setTagname(tagName);
		record.setLimittype(limitType);
		record.setStarttime(DateUtils.parseDate(startTime));
		record.setEndtime(DateUtils.parseDate(endTime));
		record.setUserid(userid);
		recordMapper.deleteOverReturnRecordByBean(record);
	}
	
	public List<OverrunRecordSelf> getUpList(List<TagValue> list,Float limitval,Float endLimitval) {
		 int flag = 0;
		 List<OverrunRecordSelf> recordList = new ArrayList<OverrunRecordSelf>();
		 OverrunRecordSelf record  = new OverrunRecordSelf();
		  	TagValue end =list.get(list.size()-1);
			if (limitval.floatValue() != endLimitval.floatValue()) {
				for (TagValue tagValue : list) {
					Float value = tagValue.getValue();
				if (value > limitval && flag == 0 && value <= endLimitval) {
					Date tagbeginTime = DateUtils.parseDate(tagValue.getTime());
					record.setStarttime(tagbeginTime);
					flag = 1;
				}
				if ((value <= limitval || value >= endLimitval) && flag == 1) {
					Date tagendTime = DateUtils.parseDate(tagValue.getTime());
					record.setEndtime(tagendTime);
					recordList.add(record);
					record = new OverrunRecordSelf();
					flag = 0;
				}}

				if (end.getValue() > limitval && end.getValue() < endLimitval) {
					Date tagendTime = DateUtils.parseDate(end.getTime());
					record.setEndtime(tagendTime);
					recordList.add(record);
				}

			} else {
				for (TagValue tagValue : list) {
					Float value = tagValue.getValue();
				if (value > limitval && flag == 0) {
					Date tagbeginTime = DateUtils.parseDate(tagValue.getTime());
					record.setStarttime(tagbeginTime);
					flag = 1;
				}
				if ((value <= limitval) && flag == 1) {
					Date tagendTime = DateUtils.parseDate(tagValue.getTime());
					record.setEndtime(tagendTime);
					recordList.add(record);
					record = new OverrunRecordSelf();
					flag = 0;
				}
				}
				if (end.getValue() > limitval) {
					Date tagendTime = DateUtils.parseDate(end.getTime());
					record.setEndtime(tagendTime);
					recordList.add(record);
				}
			
		}
	     	
		 return recordList;
	}
	
	public List<OverrunRecordSelf> getDongtaiUpList(List<TagValue> list,List<TagValue> dongtailist) {
		 int flag = 0;
		 List<OverrunRecordSelf> recordList = new ArrayList<OverrunRecordSelf>();
		 OverrunRecordSelf record  = new OverrunRecordSelf();
		  	TagValue end =list.get(list.size()-1);
		  	TagValue limitend =dongtailist.get(list.size()-1);
		  	int i =0;
		  	
				for (TagValue tagValue : list) {
					Float limitval = dongtailist.get(i).getValue();
					i++;
					Float value = tagValue.getValue();
				if (value > limitval && flag == 0) {
					Date tagbeginTime = DateUtils.parseDate(tagValue.getTime());
					record.setStarttime(tagbeginTime);
					record.setLimitval(limitval);
					flag = 1;
				}
				if ((value <= limitval) && flag == 1) {
					Date tagendTime = DateUtils.parseDate(tagValue.getTime());
					record.setEndtime(tagendTime);
					record.setLimitval(limitval);
					recordList.add(record);
					record = new OverrunRecordSelf();
					flag = 0;
				}
				}
				if (end.getValue() > limitend.getValue()) {
					Date tagendTime = DateUtils.parseDate(end.getTime());
					record.setEndtime(tagendTime);
					record.setLimitval(limitend.getValue());
					recordList.add(record);
				}
			
		       
	     	
		 return recordList;
	}
	
	
	
	public List<OverrunRecordSelf> getDownList(List<TagValue> list,Float limitval,Float endLimitval) {
		 int flag = 0;
		 List<OverrunRecordSelf> recordList = new ArrayList<OverrunRecordSelf>();
		 OverrunRecordSelf record  = new OverrunRecordSelf();
		if(limitval.floatValue() == endLimitval.floatValue()) {
			 for (TagValue tagValue : list) {
			     	Float value =	tagValue.getValue();
			     	 if(value<limitval && flag==0) {
			     		Date tagbeginTime=DateUtils.parseDate(tagValue.getTime());
			     		 record.setStarttime(tagbeginTime);
			     		 flag=1;
			     	 }
			     	 if(value>=limitval && flag==1) {
			     		Date tagendTime=DateUtils.parseDate(tagValue.getTime());
			     	    record.setEndtime(tagendTime);
			     		recordList.add(record);
			     		record = new OverrunRecordSelf();
			     		flag=0;
			     	 }
				}
				 TagValue end =list.get(list.size()-1);
				 if(end.getValue()<limitval) {
					Date tagendTime=DateUtils.parseDate(end.getTime());
					 record.setEndtime(tagendTime);
			     		recordList.add(record);
				 }
		}else {
			 for (TagValue tagValue : list) {
			     	Float value =	tagValue.getValue();
			     	 if(value<limitval && flag==0 && value>endLimitval) {
			     		Date tagbeginTime=DateUtils.parseDate(tagValue.getTime());
			     		 record.setStarttime(tagbeginTime);
			     		 flag=1;
			     	 }
			     	 if((value>=limitval || value <endLimitval ) && flag==1) {
			     		Date tagendTime=DateUtils.parseDate(tagValue.getTime());
			     	    record.setEndtime(tagendTime);
			     		recordList.add(record);
			     		record = new OverrunRecordSelf();
			     		flag=0;
			     	 }
				}
				 TagValue end =list.get(list.size()-1);
				 if(end.getValue()<=limitval && end.getValue()>endLimitval) {
					Date tagendTime=DateUtils.parseDate(end.getTime());
					 record.setEndtime(tagendTime);
			     		recordList.add(record);
				 }
			
		}
	
		 return recordList;
	}
	
	



	 
}
